<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="平台类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['type', validatorRules.type]" placeholder="请选择1：安卓、2：iOS"/>-->
              <div style="padding-left: 50px;">
                <a-radio-group v-model="value" v-decorator="['type']">
                  <a-radio :value="1">
                    安卓
                  </a-radio>
                  <a-radio :value="2">
                    iOS
                  </a-radio>
                </a-radio-group>
              </div>

            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="版本名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入版本名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="版本号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['number', validatorRules.number]" placeholder="请输入版本号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="版本更新内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['content', validatorRules.content]" rows="4" placeholder="请输入版本更新内容"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新链接：vivo" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['urlVivo', validatorRules.urlVivo]" placeholder="请输入更新链接：vivo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新链接：oppo" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['urlOppo', validatorRules.urlOppo]" placeholder="请输入更新链接：oppo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新链接：mi" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['urlMi', validatorRules.urlMi]" placeholder="请输入更新链接：mi"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新链接：huawei" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['urlHuawei', validatorRules.urlHuawei]" placeholder="请输入更新链接：huawei"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新链接：samsung" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['urlSamsung', validatorRules.urlSamsung]" placeholder="请输入更新链接：samsung"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新链接：meizu" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['urlMeizu', validatorRules.urlMeizu]" placeholder="请输入更新链接：meizu"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新链接：apple" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['urlApple', validatorRules.urlApple]" placeholder="请输入更新链接：apple"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="强制升级:" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <div style="padding-left: 50px;">
                  <a-radio-group v-model="value1" v-decorator="['upgrade']">
                    <a-radio :value="0">
                      不强制
                    </a-radio>
                    <a-radio :value="1">
                      强制
                    </a-radio>
                  </a-radio-group>
                </div>
            </a-form-item>
          </a-col>


          <a-col :span="24">
            <a-form-item label="屏蔽隐藏：" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div style="padding-left: 50px;">
                <a-radio-group v-model="value1" v-decorator="['shield']">
                  <a-radio :value="0">
                   隐藏
                  </a-radio>
                  <a-radio :value="1">
                   显示
                  </a-radio>
                </a-radio-group>
              </div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否支付：" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div style="padding-left: 50px;">
                <a-radio-group v-model="value1" v-decorator="['pay']">
                  <a-radio :value="0">
                    不支付
                  </a-radio>
                  <a-radio :value="1">
                   支付
                  </a-radio>
                </a-radio-group>
              </div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择创建时间" v-decorator="['createTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择修改时间" v-decorator="['updateTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@comp/jeecg/JFormContainer'
  import JDate from '@comp/jeecg/JDate'
  import JDictSelectTag from "@comp/dict/JDictSelectTag"

  export default {
    name: 'VersionForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        // value:'1',
        // value1:'0',
        confirmLoading: false,
        validatorRules: {
        type: {
        rules: [
            { required: true, message: '请输入1：安卓、2：iOS!'},
        ]
        },
        name: {
        rules: [
            { required: true, message: '请输入版本名称!'},
        ]
        },
        number: {
        rules: [
            { required: true, message: '请输入版本号!'},
        ]
        },
        content: {
        rules: [
            { required: true, message: '请输入版本更新内容!'},
        ]
        },
        urlVivo: {
        rules: [
            { required: true, message: '请输入更新链接：vivo!'},
        ]
        },
        urlOppo: {
        rules: [
            { required: true, message: '请输入更新链接：oppo!'},
        ]
        },
        urlMi: {
        rules: [
            { required: true, message: '请输入更新链接：mi!'},
        ]
        },
        urlHuawei: {
        rules: [
            { required: true, message: '请输入更新链接：huawei!'},
        ]
        },
        urlSamsung: {
        rules: [
            { required: true, message: '请输入更新链接：samsung!'},
        ]
        },
        urlMeizu: {
        rules: [
            { required: true, message: '请输入更新链接：meizu!'},
        ]
        },
        urlApple: {
        rules: [
            { required: true, message: '请输入更新链接：apple!'},
        ]
        },
        upgrade: {
        rules: [
            { required: true, message: '请输入强制升级： 0 不强制 1 强制!'},
        ]
        },
        shield: {
        rules: [
            { required: true, message: '请输入屏蔽隐藏：0 隐藏 1 显示!'},
        ]
        },
        pay: {
        rules: [
            { required: true, message: '请输入是否支付：0 不支付；1 支付!'},
        ]
        },
},
        url: {
          add: "/version/version/add",
          edit: "/version/version/edit",
          queryById: "/version/version/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'type','name','number','content','urlVivo','urlOppo','urlMi','urlHuawei','urlSamsung','urlMeizu','urlApple','upgrade','shield','pay','createTime','updateTime'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'type','name','number','content','urlVivo','urlOppo','urlMi','urlHuawei','urlSamsung','urlMeizu','urlApple','upgrade','shield','pay','createTime','updateTime'))
      },
    }
  }
</script>